package com.site.servlet;

import com.alibaba.fastjson.JSON;
import com.site.dao.EmployeeRoleDao;
import com.site.dao.impl.EmployeeRoleDaoImpl;
import com.site.pojo.Employee;
import com.site.pojo.EmployeeRole;
import com.site.pojo.Role;
import com.site.service.EmployeeRoleService;
import com.site.service.EmployeeService;
import com.site.service.RoleService;
import com.site.service.impl.EmployeeRoleServiceImpl;
import com.site.service.impl.EmployeeServiceImpl;
import com.site.service.impl.RoleServiceImpl;
import com.site.utils.CommonResult;
import com.site.utils.JDBCUtils;
import com.site.vo.UserControlVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

@WebServlet("/usercontrol.do")
public class UserControlServlet extends HttpServlet{
    private EmployeeService employeeService = new EmployeeServiceImpl();
    private RoleService roleService = new RoleServiceImpl();
    private PrintWriter out = null;
    private EmployeeRoleService employeeRoleService = new EmployeeRoleServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=UTF-8");
        String opr = req.getParameter("opr");
        try {
            out = resp.getWriter();
            switch (opr){
                case "selectAll":
                    out.write(JSON.toJSONString(getAll(req,resp)));
                    break;
                case "getallrole":
                    out.write(JSON.toJSONString(getAllRole(req,resp)));
                    break;
                case "checkUser":
                    out.write(JSON.toJSONString(checkUser(req,resp)));
                    break;
                case "addUser":
                    out.write(JSON.toJSONString(addUser(req,resp)));
                    break;
                case "delUser":
                    out.write(JSON.toJSONString(delUser(req,resp)));
                    break;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            out.flush();
            out.close();
        }
    }
    /*获取全部人员*/
    public CommonResult getAll(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        return  CommonResult.success(employeeService.selectUserVO());
    }
    /*获取所有角色*/
    public CommonResult getAllRole(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        return CommonResult.success(roleService.selectAll());
    }
    /*查重*/
    public CommonResult checkUser(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        String username = req.getParameter("userName");
        System.out.println(username);
        Employee employee = new Employee();
        employee.setEname(username);
        return CommonResult.success(employeeService.selectByname(employee));
    }
    /*添加*/
    public CommonResult addUser(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        Long eid = JDBCUtils.selectId();
        System.out.println("出现了"+req.getParameter("userName"));
        String ename = req.getParameter("userName");
        String epassword = JDBCUtils.getPassword();
        String sex = req.getParameter("sex");
        String nation = req.getParameter("nationality");
        String date = req.getParameter("date")+" 00:00:000";
        String phone = req.getParameter("phone");
        String address = req.getParameter("address");
        String rolename = req.getParameter("rolename");
        System.out.println(eid+""+ename+""+epassword+""+sex+""+nation+""+date+""+phone+""+address+""+rolename);
        Employee employee = new Employee();
        employee.setEid(eid);
        employee.setEname(ename);
        employee.setEpassword(epassword);
        employee.setSex(sex);
        employee.setNationality(nation);
        employee.setPhone(phone);
        employee.setBirthday(Timestamp.valueOf(date));
        employee.setAddress(address);
        employee.setState((long) 1);
        employee.setEmployDate(JDBCUtils.getDate());

        EmployeeRole employeeRole = new EmployeeRole();
        employeeRole.setEid(eid);

        Role role = new Role();
        role.setRoleName(rolename);
        System.out.println(roleService.getEmployeeRoleRid(role));
        employeeRole.setRid((long) roleService.getEmployeeRoleRid(role));
        boolean b = employeeService.addEmployee(employee, employeeRole);
        System.out.println(b);
        return CommonResult.success(b);
    }

    /*删除用户*/
    public CommonResult delUser(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        Long eid = Long.valueOf(req.getParameter("eid"));
        Employee employee = new Employee();
        employee.setEid(eid);
        EmployeeRole employeeRole = new EmployeeRole();
        employeeRole.setEid(eid);
        return CommonResult.success(employeeService.delEmplyee(employee,employeeRole));
    }
}
